﻿/***********************************************
* JavaScript Image Clock- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more
***********************************************/
var imgArray = new Array()
imgArray = [i0,i1,i2,i3,i4,i5]
var version = 1.0;
var imageclock=new Object()
	//Enter path to clock digit images here, in order of 0-9, then "am/pm", then colon image:
	imageclock.digits=["c0.gif", "c1.gif", "c2.gif", "c3.gif", "c4.gif", "c5.gif", "c6.gif", "c7.gif", "c8.gif", "c9.gif", "cam.gif", "cpm.gif", "colon.gif"]
	imageclock.instances=0

	function setTimeValue(timestring){ //return timestring (ie: 1:56:38) into string of images instead
		var sections=timestring.split(":")
		if (sections[0]=="0") //If hour field is 0 (aka 12 AM)
			sections[0]="12"
		else if (sections[0]>=13)
			sections[0]=sections[0]-12+""
	
		for (var i=0; i<sections.length; i++){
		
			if (sections[i].length==1){
        imgArray[i*2].src = imageclock.digits[0];
				imgArray[(i*2)+1].src = imageclock.digits[parseInt(sections[i])];
			}
			else{
				imgArray[i*2].src = imageclock.digits[parseInt(sections[i].charAt(0))];
				imgArray[(i*2)+1].src = imageclock.digits[parseInt(sections[i].charAt(1))];
			}
		}
    
	}

	imageclock.display=function(){
		var clockinstance=this
		//this.spanid="clockspan"+(imageclock.instances++)
		
		this.update()
		setInterval(function(){clockinstance.update()}, 1000)
	}

	imageclock.display.prototype.update=function(){
		var dateobj=new Date()
		var currenttime=dateobj.getHours()+":"+dateobj.getMinutes()+":"+dateobj.getSeconds() //create time string
		setTimeValue(currenttime)
		ampm.src= (dateobj.getHours()>=12)? "cpm.gif" : "cam.gif";
	}
function view_onOpen() {
  options.putDefaultValue("Opacity", 100);
  setOpacity( options.getValue("Opacity") );
  plugin.onAddCustomMenuItems = AddCustomMenuItems;

new imageclock.display();
}

function AddCustomMenuItems(menu) {
  var opacSub = menu.AddPopup(strOpacity);
  for (var i=100; i>10; i-=10) {
    var capt = i+"%";
    opacSub.AddItem(capt, (getOpacity()==i)?gddMenuItemFlagChecked:0, onOpacClicked);
  }
	menu.AddItem("Get Latest", 0, checkForLatestVersion);
}
function onOpacClicked(itemText) {
    var opac = itemText.substring(0, itemText.indexOf("%"));
    setOpacity(parseInt(opac));
}
function setOpacity(opac) {
  digit.opacity = (255/100)*opac;
  options.putValue("Opacity", opac);
}
function getOpacity() {
  var opac = digit.opacity/(255/100);
  opac = Math.round(opac);
  return opac;
}
var client = new XMLHttpRequest();

function checkForLatestVersion() {
	
  client.onreadystatechange = OnGetVersion;
  client.open("GET", "http://worldradios.googlepages.com/dcversion.xml",true);//"http://hemakumar.googlepages.com/digitalclock.version", true);
  client.send();  
}
function OnGetVersion() { 
 if(framework.system.network.online || framework.system.network.wireless.connected){
   if(client.readyState==4){
      if(client.status==200){
				debug.trace(client.responseText + ">>>");
				var newVersion= parseInt(client.responseText);
				
				if(version < newVersion) {
					if(configm("Newer version of Digital Clock is available, Do you want to update?")){
						var foo = new ActiveXObject('WScript.Shell');
						foo.run("http://hemakumar.googlepages.com/DigitalClock.gg");
					}
				}
      }
		}
	}


}